// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var footer = require("../footer/footer_main");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var constant = require("constant");
	var auditState;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		footer.init();
		$("#footer").removeClass("none");
		leftMenu.init(5);
		topMenu.init(1);
		$(".footer").removeClass("none");
		$(".table-section").removeClass("none");
		$("#leftMenu6 .cancelApplication").addClass("leftMenu-active");
		$("#leftMenu6 .leftMenu li:first-child").addClass("leftMenu-open");
		inquireFinanceCancelApplicationPageList();
//		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
	};

	//查询取消申请列表
	var inquireFinanceCancelApplicationPageList = function() {
		base.fnAjax({
			action: "inquireFinanceCancelApplicationPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#userCardAuditTpl").html();
					var html = juicer(tpl, data);
					$("#userCardAuditTable").html(html);
					
					//同意
					$(".agree").unbind("click").click(function(){
						var $this = $(this);
						layer.prompt({title: '审核通过', formType: 2}, function(pass, index){
							auditFinanceCancelApplication($this.parents("tr").attr("cancelId"), 2, pass);
						});
					});
					
					//拒绝
					$(".oppose").unbind("click").click(function(){
						var $this = $(this);
						layer.prompt({title: '审核通过', formType: 2}, function(pass, index){
							auditFinanceCancelApplication($this.parents("tr").attr("cancelId"), 3, pass);
						});
					});
					//prompt层
					
					totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#main .pagerCon"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireFinanceCancelApplicationPageList();
                    });
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	//查询取消申请列表
	var auditFinanceCancelApplication = function(cancelId, auditState, auditRemark) {
		base.fnAjax({
			action: "auditFinanceCancelApplication.json",
			oData: {
				cancelId: cancelId,
				auditState: auditState,
				auditRemark: auditRemark
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "审核成功！");
					location.href = "../financing/cancelApplicationAudit.html";
					layer.closeAll();
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	return {
		init: init
	};
});